/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-01-08
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.ShopCategory;
import com.rzico.basics.entity.Supplier;
import com.rzico.core.entity.SysUser;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.basics.entity.ShopCategory;
import com.rzico.basics.service.ShopCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 店铺分类控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-01-08
 */
@Api(description = "店铺分类接口")
@RestController
@RequestMapping("/admin/shopCategory")
public class ShopCategoryAdminController extends BaseController {

    @Autowired
    private ShopCategoryService shopCategoryService;

    /**
     * 分页查询产品分类
     *
     * @return
     */
    @ApiOperation("分页查询产品分类")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "分类ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "scopeType", value = "经营范围 {0:其他,1:餐饮,2:酒店,3:名厨,4.餐车}", dataType = "Long", paramType = "query")
    })
    public CommResult<ShopCategory> list(Long id,Long scopeType) {
        Map<String, Object> params = new HashMap<String, Object>();

        if (null != id){
            params.put("treePath", id);
        }

        if (null != scopeType){
            params.put("scopeType", scopeType);
        }

        params.put("sortField", "tree_path");
        params.put("sortType", "asc");
        List<ShopCategory> list = shopCategoryService.selectTree(params);
        return CommResult.success(list);

    }

    /**
     * 查询单条产品分类
     *
     * @return
     */
    @ApiOperation("查询单条产品分类")
    @GetMapping("/find/{id}")
    public CommResult<ShopCategory> find(@PathVariable String id) {
        ShopCategory result = shopCategoryService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("shopCategory",result);
        return CommResult.success(result);

    }

    /**
     * 保存产品分类
     *
     * @param shopCategory
     * @return
     */
    @ApiOperation("保存产品分类")
    @PostMapping("/save")
    public CommResult<ShopCategory> save(@RequestBody ShopCategory shopCategory) {

        shopCategory.setCreateDate(new Date());

        int affectCount = shopCategoryService.insertUseGeneratedKeys(shopCategory);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        ShopCategory result = shopCategoryService.findById(shopCategory.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除产品分类
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除产品分类", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除产品分类,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<ShopCategory> del(@PathVariable String ids) {

        shopCategoryService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新产品分类
     *
     * @param shopCategory
     * @return
     */
    @Log(desc = "更新产品分类", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新产品分类")
    @PostMapping("/update")
    public CommResult<ShopCategory> update(@RequestBody ShopCategory shopCategory) {

        int affectCount = shopCategoryService.updateByPrimaryKeySelective(shopCategory);
        if (affectCount <= 0){
            return CommResult.error();
        }
        ShopCategory result = shopCategoryService.findById(shopCategory.getId());
        return CommResult.success(result);
    }


}
